<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-04 16:53:56
 * @LastEditTime: 2023-09-22 14:33:43
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 加料单
 */

namespace App\Http\Services\Finance;


use App\Http\Services\Service;

use App\Repositories\FinanceBalanceInitializationRepository;
use App\Repositories\FinanceVoucherRepository;
use App\Repositories\SubjectRepository;
use Illuminate\Support\Facades\DB;

/**
 * @property FinanceBalanceInitializationRepository $repository
 * @property SubjectRepository $subject
 * @property FinanceVoucherRepository $voucher
 */
class InitializationService extends Service
    {

    public function boot(

        FinanceBalanceInitializationRepository $repository,
        SubjectRepository $subject,
        FinanceVoucherRepository $voucher

    ) {
        $this->repository = $repository;
        $this->subject    = $subject;
        $this->voucher    = $voucher;
        }

    public function browser()
        {
        return $this->repository->condition()->getGrid();
        }
    public function getSubjectAmount($subject)
        {
        $merge_data                 = $subject->toArray();
        $merge_data['prior_amount'] = $this->repository
            ->where("date", "197001")
            ->where("subject_id", $subject->id)
            ->sum("amount");

        if ($subject->children->count() > 0) {
            $merge_data['children'] = $subject->children->map(function ($v) use (&$merge_data)
                {
                $new                        = $this->getSubjectAmount($v);
                $merge_data['prior_amount'] += $new['prior_amount'];
                return $new;
                });
            }
        return $merge_data;

        }
    public function list()
        {

        $rows = $this->subject->getMenuList()->map(function ($v)
            {
            return $this->getSubjectAmount($v);
            });

        return array_values($rows->toArray());
        }

    protected function save()
        {
        $gdata = $this->repository->autosave(request()->toArray());

        if ($gdata->voucher) {
            [$debit_id, $credit_id, $invoid_id] = $gdata->voucher->details->map(function ($v)
                {
                return $v->id;
                });
            }
        $subject = $gdata->subject;

        if ($subject->direct_id == -1) {
            //借 实收资本
            $details[] = $this->voucher->mergeVoucher(3001, "期初-{$subject->name}", $gdata->amount, 0, $gdata->toArray(), $debit_id ?? 0);
            //贷 期初科目
            $details[] = $this->voucher->mergeVoucher($gdata->subject_id, "期初-{$subject->name}", 0, $gdata->amount, $gdata->toArray(), $credit_id ?? 0);

            }
        else {
            //借 期初科目
            $details[] = $this->voucher->mergeVoucher($gdata->subject_id, "期初-{$subject->name}", $gdata->amount, 0, $gdata->toArray(), $debit_id ?? 0);
            //贷 实收资本
            $details[] = $this->voucher->mergeVoucher(3001, "期初-{$subject->name}", 0, $gdata->amount, $gdata->toArray(), $credit_id ?? 0);
            }
        if (is_numeric($subject->invoice_amount) && $subject->invoice_amount != 0)
            $details[] = FinanceVoucherRepository::mergeVoucher($subject->state_id == -1 ? "2202" : "1122", '期初票', 0, 0, array_merge($gdata->toArray(), [
                'material_id'    => 0,
                'quantity'       => 0,
                'price'          => 0,
                'invoice_amount' => $subject->invoice_amount
            ]), $invoid_id ?? 0, 1);
        $this->voucher->CreateVoucher($gdata->billdate, 1, $details, $gdata, $gdata->voucher?->id ?? 0);
        $subject->initialization_id = $gdata->id;
        $subject->save();
        return $gdata;

        }
    public function delete()
        {
        return $this->repository->whereIn("id", $this->getRequestIds())->get()->filter(function ($v)
            {
            $this->voucher->RemoveVoucher($v);
            $v->delete();
            });
        }
    }